package com.easybuilder.core.rbac.controller;

import com.easybuilder.common.base.controller.BaseController;
import com.easybuilder.common.utils.StringUtil;
import com.easybuilder.core.rbac.dto.SysDeptDTO;
import com.easybuilder.core.rbac.entity.SysDept;
import com.easybuilder.core.rbac.service.SysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 *
 *
 * @author 张兴帅
 * @projectName easy-builder
 * @packageNmae com.easybuilder.core.rbac.controller
 * @fileNmae SysDeptController
 * @date 2023-5-16
 * @copyright 华能信息技术有限公司
 * @since 0.0.1
 */
@RestController
@RequestMapping("/sys/dept")
public class SysDeptController extends BaseController{
    @Autowired
    private SysDeptService deptService;

    @PostMapping("/save")
    public Result<?> save(@RequestBody SysDept sysDept){
        if (sysDept == null) {
            return BadRequest();
        }
        deptService.save(sysDept);
        return OK("保存成功");
    }

    @GetMapping("/load/{pid}")
    public Result<Object> loadByPid(@PathVariable(name = "pid")String pid) {
        List<SysDeptDTO> list = new ArrayList<>();
        if (StringUtil.isNull(pid)){
            list = deptService.loadByPid(null);
        } else {
            list = deptService.loadByPid(Long.valueOf(pid));
        }
        return OK(list);
    }


}